package com.igroupes.ovineherd.model.attribute.number;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.NumberStyle;
import com.igroupes.ovineherd.model.Style.YearStyle;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.mysql.cj.MysqlType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

/**
 * 年份
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class YearModelData extends NumberBasicAttribute {

    private static Integer LENGTH = 4;
    public static final Integer MAXAGE = 2021;
    public static final Integer MINAGE = 2000;
    public static final String MINLABEL = "最小年份";
    public static final String MAXLABEL = "最大年份";

    public YearModelData() {
        super();
        this.minValue = new NumberStyle(MINLABEL,YearModelData.MINAGE, false, YearModelData.MINAGE);
        this.maxValue = new NumberStyle(MAXLABEL, YearModelData.MAXAGE, false, YearModelData.MINAGE, YearModelData.MAXAGE);
    }

    public Boolean verifyField(YearModelData json) {
        return super.verifyField(json);
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        YearModelData yearModelData = (YearModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setLength(LENGTH);
        fieldQuery.setType(MySqlTypeKey.BIGINT.getCode());
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("minValue",new NumberStyle(MINLABEL, modelDataJsonAttributeQuery.getJson().get("minValue"), false, YearModelData.MINAGE));
        jsonObject.set("maxValue",new NumberStyle(MAXLABEL, modelDataJsonAttributeQuery.getJson().get("maxValue"), false, YearModelData.MINAGE, YearModelData.MAXAGE));
        return jsonObject;
    }


    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        String minValue = Convert.toStr(jsonObject.get("minValue"));
        String maxValue = Convert.toStr(jsonObject.get("maxValue"));
        return new JSONObject(new YearStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
                minValue, maxValue
        ), false);
    }
}
